﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Koonsoft.HYCRM.BLL;
using Koonsoft.HYCRM.Model;

public partial class Deliver_HandleOrderForCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    /// <summary>
    /// 显示订单数据
    /// </summary>
    private void BindData()
    {
        if (Request.QueryString["key"] != null)
        {
            var orderid = Request.QueryString["key"];

            if (!string.IsNullOrEmpty(orderid))
            {
                if (orderid.Length == 10)
                {
                    Label1.Text = orderid;
                    OrderID.Value = orderid;

                    var order = new OrderBLL().GetOrderInfoByOrderID(orderid);

                    var client = new ClientBLL().GetClientInfoByOrderID(orderid);

                    if (client != null && order != null)
                    {
                        //if (DateTime.Now.AddDays(-20) <= order.OrderTime)
                        //{
                        //    ClientName.Text = client.Name;
                        //    if (string.IsNullOrEmpty(client.Telephone.Trim()))
                        //    {
                        //        Telephone.Text = string.Empty;
                        //    }
                        //    else
                        //    {
                        //        Telephone.Text = EncryptHelper.Decrypt(client.Telephone, "3.141592");
                        //    }
                        //    if (string.IsNullOrEmpty(client.Mobile.Trim()))
                        //    {
                        //        Mobile.Text = string.Empty;
                        //    }
                        //    else
                        //    {
                        //        Mobile.Text = EncryptHelper.Decrypt(client.Mobile, "3.141592");
                        //    }
                        //    if (string.IsNullOrEmpty(client.Address.Trim()))
                        //    {
                        //        Address.Text = string.Empty;
                        //    }
                        //    else
                        //    {
                        //        Address.Text = EncryptHelper.Decrypt(client.Address, "3.141592");
                        //    }

                        //    AgentCompany.Text = client.AgentCompanyName;
                        //}
                        //else
                        //{
                            ClientName.Text = client.Name;
                            Telephone.Text = client.Telephone;
                            Mobile.Text = client.Mobile;
                            Address.Text = EncryptHelper.Decrypt(client.Address, "3.141592");
                            AgentCompany.Text = client.AgentCompanyName;
                        //}
                    }
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // 转入分行快递，发送给客户
        if (Request.QueryString["key"] != null)
        {
            if (Request.QueryString["key"].Trim().Length == 10)
            {
                var orderID = Request.QueryString["key"].Trim();

                // 写入订单轨迹
                var empolyeeID = new EmpolyeeBLL().GetEmpolyeeInfoByLoginName(User.Identity.Name).EmpolyeeID;
                new OrderTrackBLL().AddTrack(orderID, empolyeeID, Koonsoft.HYCRM.Model.OrderStatus.BranchDeliver, 1, TextBox1.Text.Trim());

                // 通知订单状态改变
                new OrderBLL().ChangeOrderStatusByOrderID(orderID, Koonsoft.HYCRM.Model.OrderStatus.Finished);

                Response.Redirect("OrdersForCustomers.aspx");
            }
        }
    }
}